package com.mrd.frame.service.impl;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mrd.frame.dao.AccessConfigDao;
import com.mrd.frame.dao.ConfigDao;
import com.mrd.frame.domain.AccessConfig;
import com.mrd.frame.domain.Config;
import com.mrd.frame.redis.JedisClientClusterImpl;
import com.mrd.frame.service.AccessConfigService;
import com.mrd.frame.service.ConfigService;

@Service
public class AccessConfigServiceImpl implements AccessConfigService{
	@Autowired
	private AccessConfigDao accessConfigDao;
	@Autowired
	private ConfigService configService;
	@Autowired
	private JedisClientClusterImpl jedisClientClusterImpl;
	@Autowired
	private ConfigDao configDao;
	
	@Override
	public int deleteByPrimaryKey(String accessConfigId) {
		return accessConfigDao.deleteByPrimaryKey(accessConfigId);
	}

	@Override
	public int insert(AccessConfig record) {
		String userKey = record.getUserKey();
		if(userKey == null){
			return -2;
		}else if(configService.selectByUserKey(userKey) == null){
			Config config = new Config();
			config.setUserKey(userKey);
			config.setStatus("1");
			configService.insert(config);
		}else if(selectByUserKeyAndPayChannel(userKey,record.getPayChannel()) != null){
			return -3;//支付渠道已存在
		}
		int i = accessConfigDao.insert(record);
		if(i > 0){
			Config config = configDao.selectByUserKey(userKey);
			jedisClientClusterImpl.set("config_" + userKey, config, null, TimeUnit.MILLISECONDS);
		}
		return i;
	}

	@Override
	public int insertSelective(AccessConfig record) {
		String userKey = record.getUserKey();
		if(userKey == null){
			return -1;
		}
		if(configService.selectByUserKey(userKey) == null){
			Config config = new Config();
			config.setUserKey(userKey);
			config.setStatus("1");
			configService.insert(config);
		}
		int i = accessConfigDao.insertSelective(record);
		if(i > 0){
			Config config = configDao.selectByUserKey(userKey);
			jedisClientClusterImpl.set("config_" + userKey, config, null, TimeUnit.MILLISECONDS);
		}
		return i;
	}

	@Override
	public AccessConfig selectByPrimaryKey(String accessConfigId) {
		return accessConfigDao.selectByPrimaryKey(accessConfigId);
	}

	@Override
	public int updateByPrimaryKeySelective(AccessConfig record) {
		int i = accessConfigDao.updateByPrimaryKeySelective(record);
		if(i > 0){
			String userKey = record.getUserKey();
			Config config = configDao.selectByUserKey(userKey);
			jedisClientClusterImpl.set("config_" + userKey, config, null, TimeUnit.MILLISECONDS);
		}
		return i;
	}

	@Override
	public int updateByPrimaryKey(AccessConfig record) {
		int i = accessConfigDao.updateByPrimaryKey(record);
		if(i > 0){
			String userKey = record.getUserKey();
			Config config = configDao.selectByUserKey(userKey);
			jedisClientClusterImpl.set("config_" + userKey, config, null, TimeUnit.MILLISECONDS);
		}
		return i;
	}

	@Override
	public AccessConfig selectByUserKeyAndPayChannel(String userKey, String payChannel) {
		if(userKey == null || payChannel == null){
			return null;
		}
		Config config = configService.selectByUserKey(userKey);
		List<AccessConfig> accessConfigList = config.getAccessConfigList();
		AccessConfig accessConfig = null; 
		for(AccessConfig ac : accessConfigList){
			if(ac.getPayChannel().equals(payChannel)){
				accessConfig = ac;
				break;
			}
		}
		return accessConfig;
	}

	@Override
	public int deleteByPrimaryKeys(String ids) {
		String[] id = ids.split(",");
		return accessConfigDao.deleteByPrimaryKeys(id);
	}

	@Override
	public List<AccessConfig> selectSelective(AccessConfig accessConfig) {
		return accessConfigDao.selectSelective(accessConfig);
	}
    
    /**
     * 查询所有Config配置，不包括子表和白名单
     * @author han 
     * @return Config
     */
	@Override
    public void initAccessConfig(){
		List<Config> list = configDao.selectAllConfig();
		if(list!=null && list.size()>0){
			for(Config config :list){
				String userKey = config.getUserKey();
				if(StringUtils.isNotEmpty(userKey)){
					Config itemConf = configDao.selectByUserKey(userKey);
					jedisClientClusterImpl.delete("config_" + userKey);
					jedisClientClusterImpl.set("config_" + userKey, itemConf, null, TimeUnit.MILLISECONDS);
				}
			}
		}
    }
}